package sapling.api;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sapling.dao.UserDao;
import sapling.model.User;
import sapling.utils.Md5Util;
import sapling.utils.Msg;

import java.util.Objects;

/**
 * Created by Administrator on 2017/6/27.
 */
@RestController
@RequestMapping("/api/user")
public class UserApi {
    @Autowired
    private UserDao userDao;

    @ApiOperation(value = "用户登陆", notes = "根据用户名和密码登陆")
    @GetMapping("/login")
    public Msg<User> login(@RequestParam String username, @RequestParam String password) {
        Msg<User> msg;
        User user= userDao.findOneByUsername(username);
        if(Objects.nonNull(user)){
            if(Md5Util.getMd5(password).equals(user.getPassword())){
                msg=Msg.newMsg(user);
            }else {
                msg=Msg.fail("密码错误");
            }
        }else {
            msg=Msg.fail("用户不存在");
        }
        return msg;
    }
}
